using System;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using BTOps;

public partial class BTOps_SendPort : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            pnlSendPortInfo.Visible = true;
            pnlSendPortDetail.Visible = true;
        }
    }

    protected void ddlApps_DataBound(object sender, EventArgs e)
    {
        if (!String.IsNullOrEmpty(Request.QueryString["app"]))
        {
            string app = Request.QueryString["app"];
            if (ddlApps.Items.Contains(new ListItem(app)))
            {
                ddlApps.SelectedValue = app;
                pnlSendPortInfo.Visible = true;
                pnlSendPortDetail.Visible = true;
            }
        }
    }

    protected void SendPortDetailsDS_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
    {
        BTSendPort sendPort = (BTSendPort) e.InputParameters["sendPort"];
        if (null != sendPort)
        {
            TextBox PortName = dvwSendPort.FindControl("txtName") as TextBox;
            DropDownList TransportType = dvwSendPort.FindControl("ddlTransportType") as DropDownList;
            TextBox Address = dvwSendPort.FindControl("txtAddress") as TextBox;
            DropDownList SendPipeline = dvwSendPort.FindControl("ddlSendPipeline") as DropDownList;
            DropDownList ReceivePipeline = dvwSendPort.FindControl("ddlReceivePipeline") as DropDownList;
            TextBox SendHandlerName = dvwSendPort.FindControl("SendHandlerName") as TextBox;
            TextBox Description = dvwSendPort.FindControl("txtDescription") as TextBox;

            sendPort.ApplicationName = ddlApps.SelectedValue;
            if (null != PortName)
                sendPort.Name = PortName.Text.ToUpper();
            if (null != TransportType)
                sendPort.TransportType = TransportType.SelectedValue;
            if (null != Address)
                sendPort.Address = Address.Text;
            if (null != SendPipeline)
                sendPort.SendPipelineName = SendPipeline.SelectedValue;
            if (null != ReceivePipeline)
                sendPort.ReceivePipelineName = ReceivePipeline.SelectedValue;
            if (null != SendHandlerName)
                sendPort.SendHandlerName = SendHandlerName.Text;
            if (null != Description)
                sendPort.Description = Description.Text;

            sendPort.TransportInfo = GenerateTransportInfo(sendPort.TransportType);
        }
    }

    protected void SendPortDetailsDS_Updating(object sender, ObjectDataSourceMethodEventArgs e)
    {
        BTSendPort sendPort = (BTSendPort) e.InputParameters["sendPort"];
        sendPort.ApplicationName = ddlApps.SelectedValue;
        sendPort.TransportInfo = GenerateTransportInfo(sendPort.TransportType);
    }

    protected void dvwSendPort_DataBound(object sender, EventArgs e)
    {
        if (null != dvwSendPort.DataItem)
        {
            BTSendPort sendPort = (BTSendPort) (dvwSendPort.DataItem);
            string transportTypeData = sendPort.TransportTypeData ?? String.Empty;

            switch (sendPort.TransportType)
            {
                case "FILE":
                    txtDirectory.Text = Path.GetDirectoryName(sendPort.Address);
                    txtFileName.Text = ReadTransportDataProperty(transportTypeData, "FileName");
                    ddlCopyMode.SelectedValue = ReadTransportDataProperty(transportTypeData, "CopyMode");
                    chkAllowCache.Checked = ReadTransportDataProperty(transportTypeData, "AllowCacheOnWrite") == "-1";
                    chkUseTemp.Checked = ReadTransportDataProperty(transportTypeData, "UseTempFileOnWrite") == "-1";
                    break;
                case "FTP":
                    txtServerAddress.Text = ReadTransportDataProperty(transportTypeData, "serverAddress");
                    txtServerPort.Text = ReadTransportDataProperty(transportTypeData, "serverPort");
                    txtUserName.Text = ReadTransportDataProperty(transportTypeData, "userName");
                    txtPassword.Text = ReadTransportDataProperty(transportTypeData, "password");
                    ddlRepresentation.SelectedValue = ReadTransportDataProperty(transportTypeData, "representationType");
                    txtFolder.Text = ReadTransportDataProperty(transportTypeData, "targetFolder");
                    txtTargetFile.Text = ReadTransportDataProperty(transportTypeData, "targetFileName");
                    txtFirewallAddress.Text = ReadTransportDataProperty(transportTypeData, "firewallAddress");
                    txtFirewallPort.Text = ReadTransportDataProperty(transportTypeData, "firewallPort");
                    txtFirewallUser.Text = ReadTransportDataProperty(transportTypeData, "firewallUser");
                    txtFirewallPassword.Text = ReadTransportDataProperty(transportTypeData, "firewallPassword");
                    ddlFirewallType.SelectedValue = ReadTransportDataProperty(transportTypeData, "firewallType");
                    ddlPassiveMode.SelectedValue = ReadTransportDataProperty(transportTypeData, "passiveMode");
                    break;
                case "SQL":
                    txtConnString.Text = ReadTransportDataProperty(transportTypeData, "connectionString");
                    txtRootElementName.Text = ReadTransportDataProperty(transportTypeData, "outputRootElementName");
                    txtTargetNamespace.Text = ReadTransportDataProperty(transportTypeData, "documentTargetNamespace");
                    break;
                default:
                    break;
            }

            ToggleTransportInfoPanel(sendPort.TransportType);
        }
        else
            dvwSendPort.ChangeMode(DetailsViewMode.Insert);
    }

    protected void ddlTransportType_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList TransportType = (DropDownList) sender;
        ToggleTransportInfoPanel(TransportType.SelectedValue);
    }

    private void ToggleTransportInfoPanel(string transportType)
    {
        pnlFILEInfo.Visible = false;
        pnlFTPInfo.Visible = false;
        pnlSQLInfo.Visible = false;

        txtDirectory.Enabled = false;
        txtFileName.Enabled = false;
        ddlCopyMode.Enabled = false;
        chkAllowCache.Enabled = false;
        chkUseTemp.Enabled = false;
        txtServerAddress.Enabled = false;
        txtServerPort.Enabled = false;
        txtUserName.Enabled = false;
        txtPassword.Enabled = false;
        ddlRepresentation.Enabled = false;
        txtFolder.Enabled = false;
        txtTargetFile.Enabled = false;
        txtFirewallAddress.Enabled = false;
        txtFirewallPort.Enabled = false;
        txtFirewallUser.Enabled = false;
        txtFirewallPassword.Enabled = false;
        ddlFirewallType.Enabled = false;
        ddlPassiveMode.Enabled = false;
        txtConnString.Enabled = false;
        txtRootElementName.Enabled = false;
        txtTargetNamespace.Enabled = false;

        bool fieldEnabled = dvwSendPort.CurrentMode != DetailsViewMode.ReadOnly;
        if ((dvwSendPort.CurrentMode == DetailsViewMode.Edit) && (null == dvwSendPort.DataItem))
            fieldEnabled = false;

        switch (transportType)
        {
            case "FILE":
                txtDirectory.Enabled = fieldEnabled;
                txtFileName.Enabled = fieldEnabled;
                ddlCopyMode.Enabled = fieldEnabled;
                chkAllowCache.Enabled = fieldEnabled;
                chkUseTemp.Enabled = fieldEnabled;

                pnlFILEInfo.Visible = true;
                break;
            case "FTP":
                txtServerAddress.Enabled = fieldEnabled;
                txtServerPort.Enabled = fieldEnabled;
                txtUserName.Enabled = fieldEnabled;
                txtPassword.Enabled = fieldEnabled;
                ddlRepresentation.Enabled = fieldEnabled;
                txtFolder.Enabled = fieldEnabled;
                txtTargetFile.Enabled = fieldEnabled;
                txtFirewallAddress.Enabled = fieldEnabled;
                txtFirewallPort.Enabled = fieldEnabled;
                txtFirewallUser.Enabled = fieldEnabled;
                txtFirewallPassword.Enabled = fieldEnabled;
                ddlFirewallType.Enabled = fieldEnabled;
                ddlPassiveMode.Enabled = fieldEnabled;

                pnlFTPInfo.Visible = true;
                break;
            case "SQL":
                txtConnString.Enabled = fieldEnabled;
                txtRootElementName.Enabled = fieldEnabled;
                txtTargetNamespace.Enabled = fieldEnabled;

                pnlSQLInfo.Visible = true;
                break;
            default:
                break;
        }
    }

    private BTSendTransportInfo GenerateTransportInfo(string transportType)
    {
        BTSendTransportInfo transportInfo = new BTSendTransportInfo();
        switch (transportType)
        {
            case "FILE":
                TextBox Directory = pnlFILEInfo.FindControl("txtDirectory") as TextBox;
                TextBox FileName = pnlFILEInfo.FindControl("txtFileName") as TextBox;
                DropDownList CopyMode = pnlFILEInfo.FindControl("ddlCopyMode") as DropDownList;
                CheckBox AllowCache = pnlFILEInfo.FindControl("chkAllowCache") as CheckBox;
                CheckBox UseTemp = pnlFILEInfo.FindControl("chkUseTemp") as CheckBox;

                if (null != Directory)
                    transportInfo.Directory = Directory.Text.ToUpper();
                if (null != FileName)
                    transportInfo.FileName = FileName.Text;
                if (null != CopyMode)
                    transportInfo.CopyMode = Convert.ToInt32(CopyMode.SelectedValue);
                if (null != AllowCache)
                    transportInfo.AllowCacheOnWrite = AllowCache.Checked;
                if (null != UseTemp)
                    transportInfo.UseTempFileOnWrite = UseTemp.Checked;
                break;
            case "FTP":
                TextBox ServerAddress = pnlFTPInfo.FindControl("txtServerAddress") as TextBox;
                TextBox ServerPort = pnlFTPInfo.FindControl("txtServerPort") as TextBox;
                TextBox UserName = pnlFTPInfo.FindControl("txtUserName") as TextBox;
                TextBox Password = pnlFTPInfo.FindControl("txtPassword") as TextBox;
                DropDownList Represantation = pnlFTPInfo.FindControl("ddlRepresantation") as DropDownList;
                TextBox Folder = pnlFTPInfo.FindControl("txtFolder") as TextBox;
                TextBox TargetFileName = pnlFTPInfo.FindControl("txtTargetFileName") as TextBox;
                TextBox FirewallAddress = pnlFTPInfo.FindControl("txtFirewallAddress") as TextBox;
                TextBox FirewallPort = pnlFTPInfo.FindControl("txtFirewallPort") as TextBox;
                TextBox FirewallUser = pnlFTPInfo.FindControl("txtFirewallUser") as TextBox;
                TextBox FirewallPassword = pnlFTPInfo.FindControl("txtFirewallPassword") as TextBox;
                DropDownList FirewallType = pnlFTPInfo.FindControl("ddlFirewallType") as DropDownList;
                DropDownList PassiveMode = pnlFTPInfo.FindControl("ddlPassiveMode") as DropDownList;

                if (null != ServerAddress)
                    transportInfo.ServerAddress = ServerAddress.Text;
                if (null != ServerPort)
                    transportInfo.ServerPort = Convert.ToInt32(ServerPort.Text);
                if (null != UserName)
                    transportInfo.UserName = UserName.Text;
                if (null != Password)
                    transportInfo.Password = Password.Text;
                if (null != Represantation)
                    transportInfo.RepresentationType = Represantation.SelectedValue;
                if (null != Folder)
                    transportInfo.TargetFolder = Folder.Text;
                if (null != TargetFileName)
                    transportInfo.TargetFileName = TargetFileName.Text;
                if (null != FirewallAddress)
                    transportInfo.FirewallAddress = FirewallAddress.Text;
                if (null != FirewallPort)
                    transportInfo.FirewallPort = Convert.ToInt32(FirewallPort.Text);
                if (null != FirewallUser)
                    transportInfo.FirewallUser = UserName.Text;
                if (null != FirewallPassword)
                    transportInfo.FirewallPassword = Password.Text;
                if (null != FirewallType)
                    transportInfo.FirewallType = FirewallType.SelectedValue;
                if (null != PassiveMode)
                    transportInfo.PassiveMode = PassiveMode.SelectedValue == "Passive";
                break;
            case "SQL":
                TextBox ConnString = pnlSQLInfo.FindControl("txtConnString") as TextBox;
                TextBox RootElementName = pnlSQLInfo.FindControl("txtRootElementName") as TextBox;
                TextBox TargetNamespace = pnlSQLInfo.FindControl("txtTargetNamespace") as TextBox;

                if (null != ConnString)
                    transportInfo.ConnectionString = ConnString.Text;
                if (null != RootElementName)
                    transportInfo.OutputRootElementName = RootElementName.Text;
                if (null != TargetNamespace)
                    transportInfo.DocumentTargetNamespace = TargetNamespace.Text;
                break;
            default:
                break;
        }

        return transportInfo;
    }

    private static string ReadTransportDataProperty(string transportData, string propertyName)
    {
        try
        {
            XmlDocument configXml = new XmlDocument();
            configXml.LoadXml(transportData);
            XmlNode propertyNode = configXml.SelectSingleNode("//" + propertyName);
            if (null == propertyNode)
            {
                int start = transportData.IndexOf("&lt;" + propertyName + "&gt;") + propertyName.Length + 8;
                int end = transportData.IndexOf("&lt;/" + propertyName + "&gt;");
                int length = end - start;

                return (length > 0) ? transportData.Substring(start, length) : String.Empty;
            }
            else
                return propertyNode.InnerText;
        }
        catch
        {
            int start = transportData.IndexOf("&lt;" + propertyName + "&gt;") + propertyName.Length + 8;
            int end = transportData.IndexOf("&lt;/" + propertyName + "&gt;");
            int length = end - start;

            return (length > 0) ? transportData.Substring(start, length) : String.Empty;
        }
    }
}